package com.spring.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.spring.demo.pojo.Book;
import com.spring.demo.service.IBookService;

/**
 * Book Controller
 * 
 * @author nickel
 * @date 2019-01-25 09:30:05
 */
@Controller
@RequestMapping("/book")
public class BookController {
	@Autowired
	private IBookService bookService;

	@GetMapping("/save")
	public ModelAndView editBook(Book book, ModelAndView mv) {
		mv.addObject(book);
		mv.setViewName("book/BookEdit");
		return mv;
	}

	@PostMapping("/save")
	public ModelAndView addBook(Book book, ModelAndView mv) {
		bookService.save(book);
		mv.addObject(book);
		mv.setViewName("redirect:/book/list");
		return mv;
	}

	@RequestMapping("/list")
	public ModelAndView listBook(ModelAndView mv) {
		mv.addObject("books", bookService.list());
		mv.setViewName("book/BookList");
		return mv;
	}

}
